<template>
  <a-modal
    title="增加分包商"
    class="large-modal"
    centered
    :visible="visible"
    :confirm-loading="loading"
    @ok="handleOk"
    @cancel="visible = false"
  >
    <searchData
      ref="searchData"
      :search-data="defaultSearchList"
      @search="search"
      @reset="search"
    />
    <a-card>
      <b-table
        ref="table"
        :select-config="{ use: true, type: 'radio', rowKey }"
        :scroll="{ y: 350 }"
        :columns="localColumns"
        :request="loadData"
        @row-click="handleChange"
      />
    </a-card>
  </a-modal>
</template>
<script>
import { getSubcontractorsByProject } from '@/api/subcontractors/standing'

// this.$emit('createList', this.selectedRows),会返回选中的数组
export default {
  name: 'DialogSubcontractors',
  props: {
    //已添加的数据的名称和编码
    outTableDataMaterialKey: {
      type: Array,
      default() {
        return ['subcontractorsId', 'subcontractors']
      }
    },
    //已添加的数据
    selectedKeys: {
      type: Array,
      default() {
        return []
      }
    },
    rowKey: {
      type: String,
      default: 'subcontractorsId'
    },
    //表格请求api
    api: {
      type: [Object, Function],
      default: () => getSubcontractorsByProject
    },
    // 请求参数
    requestParams: {
      type: Object,
      default: () => {
        return {}
      }
    },
    rowConfig: {
      type: Object,
      default: () => {
        return {
          key: 'subcontractorsId',
          name: 'subcontractors'
        }
      }
    },
    // 表头
    localColumns: {
      type: [Object, Array],
      default: () => {
        return [
          {
            title: '分包商名称',
            dataIndex: 'subcontractors'
          },
          {
            title: '分部分项',
            dataIndex: 'partialName',
            width: '40%'
          },
          {
            title: '进场时间',
            dataIndex: 'entryDate'
          },
          {
            title: '状态',
            // width: 80,
            dataIndex: 'outTime',
            customRender(text) {
              return text === null ? '进场' : '出场'
            }
          }
        ]
      }
    },
    // 搜索项配置
    localSearchArr: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  data() {
    return {
      loading: false,
      visible: false,
      queryParams: {},
      loadData: async (params) => {
        let searchData = Object.assign({}, params, this.queryParams)
        // 添加额外参数
        if (Object.keys(this.requestParams)?.length > 0) {
          searchData = Object.assign({}, searchData, this.requestParams)
        }

        let res = await this.api(searchData)
        if (res.code === this.SUCCESS_CODE) {
          for (let i = 0; i < res.data.length; i++) {
            let v = res.data[i]
            let isSelect = this.selectedKeys.includes(v[this.rowConfig.key])
            if (isSelect || v?.partialAllNameList.length < 1) {
              v.disabled = true
            }
          }
          if (this.selectedRows.length > 0) {
            this.selectedRows = [res.data.find((item) => item.subcontractorsId === this.selectedRows[0]?.subcontractorsId)]
          }
          console.log(res.data)
          return { data: res.data, ...res }
        }
        return { data: [], code: 200 }
      },
      selectedRows: [],
      treeData: [],
      catalogCode: null,
      replaceFields: {
        children: 'children',
        title: 'mateClassName',
        key: 'mateClassSysNo'
      },

      expandedKeys: [],
      searchValue: '',
      autoExpandParent: true,
      dataList: [],

      outTableData: [],
      isDownload: false,
      downloadApi: false,
      downloadApiArgs: null
    }
  },
  watch: {
    visible: {
      handler(val) {
        if (val) {
          this.search()
        }
      }
    }
  },
  computed: {
    // 默认弹窗搜索配置项
    defaultSearchList() {
      let defaultList = [
        {
          name: '分包商名称',
          value: this.rowConfig.name,
          type: 'input'
        }
      ]
      return this.localSearchArr.length > 0 ? this.localSearchArr : defaultList
    }
  },
  methods: {
    /**
     * @description: 单选
     * @param {*} e：选中项
     * @return {*}
     */
    handleChange(t, e) {
      this.selectedRows = [e]
    },
    async handleOk() {
      if (this.selectedRows.length === null) {
        return this.$noticeWarning('请选择数据')
      }
      let temp = this.selectedRows
      console.log(temp)
      this.$emit('createList', temp)
      this.$nextTick(this.search)
      this.$refs.table.selectedRowKeys = []
      this.visible = false
    },
    async search(val) {
      this.queryParams = val || {}
      await this.$refs.table?.getData(null, true)
    }
  }
}
</script>
